package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.CustomerQueryObject;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.ICustomerTransferService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("customer")
@Controller
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ICustomerTransferService customerTransferService;

    @RequestMapping("potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {

        // 当前登录用户
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        // 不是超级管理员或者不是市场经理只能看自己跟踪的客户
        if (!(employee.isAdmin() || SecurityUtils.getSubject().hasRole("Market_Manager"))) {
            qo.setSellerId(employee.getId());
        } else { // 是超级管理员和市场经理才查询所有销售人员，回显在页面
            // 若是普通市场专员都没有这个功能，就没浪费数据查询了

        }
        qo.setStatus(Customer.STATUS_POTENTIAL);
        //int i = 1/0;
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //工作
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectBySystemDictionarySn("job");
        model.addAttribute("jobs", jobs);
        //来源
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectBySystemDictionarySn("source");
        model.addAttribute("sources", sources);
        //销售人
        List<Employee> sellers = employeeService.selectByRoleSn(new String[]{"Market", "Market_Manager"});
        model.addAttribute("sellers", sellers);

        //交流方式
        List<SystemDictionaryItem> traceTypes = systemDictionaryItemService.selectBySystemDictionarySn("traceType");
        model.addAttribute("traceTypes", traceTypes);

        return "customer/potentialList";
    }

    @RequestMapping("poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {

        // 当前登录用户
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("employee", employee);
        /*// 不是超级管理员或者不是市场经理只能看自己跟踪的客户
        if (!(employee.isAdmin() || SecurityUtils.getSubject().hasRole("Market_Manager"))) {
            qo.setSellerId(employee.getId());
        } else { // 是超级管理员和市场经理才查询所有销售人员，回显在页面
            // 若是普通市场专员都没有这个功能，就没浪费数据查询了

        }*/
        qo.setStatus(Customer.STATUS_POOL);
        //int i = 1/0;
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //工作
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectBySystemDictionarySn("job");
        model.addAttribute("jobs", jobs);
        //来源
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectBySystemDictionarySn("source");
        model.addAttribute("sources", sources);
        //销售人
        List<Employee> sellers = employeeService.selectByRoleSn(new String[]{"Market", "Market_Manager"});
        model.addAttribute("sellers", sellers);

        //交流方式
        List<SystemDictionaryItem> traceTypes = systemDictionaryItemService.selectBySystemDictionarySn("traceType");
        model.addAttribute("traceTypes", traceTypes);

        return "customer/poolList";
    }

	/*@RequestMapping("input")
    @RequriedPermission(name="部门编辑",expression = "customer:input")
	public String input(Long id,Model model){
		if (id!=null) {
			Customer customer = customerService.select(id);
			model.addAttribute("customer", customer);
		}
		return "customer/input";
	}*/

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            if (id != null) {
                customerService.delete(id);
            }
            return new JsonResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "删除失败");
        }
    }
    @RequestMapping("updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long customerId,int status) {
            customerService.updateStatus(customerId,status);
            return new JsonResult(true, "修改成功");

    }
    @RequestMapping("updateStatusAndSellerId")
    @ResponseBody
    public JsonResult updateStatusAndSellerId(Long customerId,int status,Long newSellerId) {
            customerService.updateStatusAndSellerId(customerId,status,newSellerId);
            return new JsonResult(true, "修改成功");

    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer) {
        try {
            if (customer.getId() != null) {
                customerService.update(customer);
            } else {
                customerService.insert(customer);
            }
            return new JsonResult(true, "新增或修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "新增或修改失败");
        }

    }
}
